import { defineStore } from "pinia";
import request from '@/utils/request.js'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
export const useUserStore = defineStore('userStore', () => {
    const users = ref([])
    const total = ref(10)
    const pageSize = ref(5)
    const currentPage = ref(1)
    const usernameSearch = ref('')
    const emailSearch = ref('')
    const addressSearch = ref('')
    const dialogVisible = ref(false)
    const userForm = ref({})
    const Ids = ref([])
    const userRouter = ref([])
    
    const getUsers = async () => {
        const {data} = await request.get('/user/page', {
            params: {
                current: currentPage.value,
                pageSize: pageSize.value,
                username: usernameSearch.value,
                email: emailSearch.value,
                address: addressSearch.value
            }
        })
        users.value = data.records
        total.value = data.total

    }
    // 根据名称获取路由
    const getVueMenu = async (flag) =>{
        const {data} = await request.get(`/role/flag/${flag}`)
        data.forEach(item => {
            userRouter.value.push({name:item.name,path:item.path,vuePage:item.vuePage})
        })
        // 存入localStorage
        localStorage.setItem('userRouter', JSON.stringify(userRouter.value))
    }
    const clearSearch = () => {
        // 重置表单
        usernameSearch.value = ''
        emailSearch.value = ''
        addressSearch.value = ''
        // 重新刷新
        getUsers()
    }
    const save =async ()=>{
        //发数据到后端
        const {data} = await request.post('/user',userForm.value)
        if(data){
            ElMessage.success('保存成功')
            dialogVisible.value = false
            getUsers()
        }else{
            console.log('error')
            ElMessage.warning('保存成功')
        }
    }
    const editUser = (row)=>{
        userForm.value = row
        dialogVisible.value =true
    }
    const deleteUser = async (id)=>{
        const {data} = await request.delete(`/user/${id}`)
        if(data){
            ElMessage.success('删除成功')
        }
        getUsers()
    }
    const handleSelectionChange= (val)=>{
        Ids.value = val.map((item)=>item.id)
    }
    const deleteUsers = async ()=>{
        const {data} = await request.delete('/user',{
            data:Ids.value
        })
        if(data){
            ElMessage.success('删除成功')
        }
        getUsers()
    }
    const exp = () =>{
        window.open('http://localhost:8888/user/export')
    }
    const handleUploadSuccess = ()=>{
        ElMessage.success('上传成功')
        getUsers()
    }

    return {
        users,
        total,
        pageSize,
        currentPage,
        usernameSearch,
        emailSearch,
        addressSearch,
        dialogVisible,
        userForm,
        userRouter,
        getUsers,
        getVueMenu,
        clearSearch,
        save,
        editUser,
        deleteUser,
        handleSelectionChange,
        deleteUsers,
        exp,
        handleUploadSuccess,
    }
})